{include file='edit.tpl'}

<style>
	#csv_fields p {
		padding-bottom: 5px;
	}
</style>

<div id='csv_fields'></div>

<script>
	{if $step == 2}
		createTabs = function() {
			addTab('csv_fields','Соответствие полей в файле');
		};
	{/if}
	
	$( function() {
		{if $step == 1}
			$('.editSave').hide();
			$('.editApply').button('option', 'label', 'Далее').button('option', 'icons', { secondary:'ui-icon-triangle-1-e' } );
			$("input[name='p[other_fields_delimiter]']").closest('tr').hide();
			$("select[name='p[fields_delimiter]']").on('dictValChange', function(e, id) {
				$("input[name='p[other_fields_delimiter]']").closest('tr').toggle(id=='other');
				if (id=='other') {
					$("input[name='p[other_fields_delimiter]']").addClass('mandatory').focus();
				} else {
					$("input[name='p[other_fields_delimiter]']").removeClass('mandatory');
				}
			} );
		{else}
			$('#tabs').tabs('remove',0);
			$('.editApply').hide();
			$('.editSave').button('option', 'label', 'Загрузить').button('option', 'icons', { primary:'ui-icon-arrowreturnthick-1-n' } );
			
			$('#csv_fields').append('<input type=hidden name="p[csv]" value="{$csv}">');
			$('#csv_fields').append('<input type=hidden name="p[has_names]" value="{$has_names}">');
			$('#csv_fields').append('<input type=hidden name="p[fields_delimiter]" value="{$fields_delimiter}">');
			
			
			var csvFields = {$csv_fields};
			var csvFieldChoose = $('<select>');
			csvFieldChoose.append( $('<option>').val('').html(' ') );
			for (var i=0; i<csvFields.length; ++i) {
				var fname = 'Поле '+(i+1);
				if ('{$has_names}' && csvFields[i]) {
					fname += ' (' + csvFields[i] + ')';
				}
				csvFieldChoose.append( $('<option>').val(i).html(fname) );
			}
			
			var editParams = {$editParams};
			for (var i=0; i<editParams.length; ++i) {
				$('#csv_fields').append( 
					$('<p>').append(editParams[i].title+': ').append( csvFieldChoose.clone().attr('name','p[fields]['+editParams[i].name+']') )
				);
			}
		{/if}
	} );
</script>